package com.itheima_06;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class MyThread implements Runnable {
    private Socket accept;
    public MyThread(Socket accept){
        this.accept = accept;
    }
    @Override
    public void run() {
        FileOutputStream fos =null;
        try {
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            fos = new FileOutputStream("C:\\Users\\wfsxy\\Desktop\\新建文件夹 (2)\\"+ UUID.randomUUID() +".jpg");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
                bos.flush();
            }
            OutputStream os = accept.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write("已上传");
            bw.newLine();
            bw.flush();
            accept.shutdownOutput();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
